package com.wmh.baseservice.robot.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.robot.entity.RobotApiUri;
import com.wmh.baseservice.robot.pojo.vo.RobotApiUriUpdateReqVO;
import com.wmh.baseservice.robot.service.RobotApiUriService;
import com.wmh.baseservice.robot.mapper.RobotApiUriMapper;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 20402
 * @description 针对表【robot_api_uri】的数据库操作Service实现
 * @createDate 2022-10-29 16:04:26
 */
@Service
public class RobotApiUriServiceImpl extends ServiceImpl<RobotApiUriMapper, RobotApiUri>
        implements RobotApiUriService {

    private void checkIsExist(Long id) {
        RobotApiUri entity = baseMapper.selectById(id);
        if (Objects.isNull(entity)) {
            throw new ServiceException("该数据不存在!");
        }
    }
    @Override
    public void commitRobotApiUri(String uri, String name) {
        RobotApiUri entity = baseMapper.selectOne(new LambdaQueryWrapperX<RobotApiUri>()
                .eq(RobotApiUri::getName, name)
                .eq(RobotApiUri::getUri, uri));

        // 更新时间
        if (!Objects.isNull(entity)) {
            entity.setUpdateTime(null);
            // 新增
        } else {
            entity = new RobotApiUri();
            entity.setUri(uri)
                    .setName(name)
                    .setEnable(1);
        }
        saveOrUpdate(entity);
    }

    @Override
    public void updateRobotApiUriById(RobotApiUriUpdateReqVO reqVO) {
        // 检查是否存在要修改的接口
        checkIsExist(reqVO.getId());

        RobotApiUri entity = new RobotApiUri();
        entity.setId(reqVO.getId())
                .setName(reqVO.getName())
                .setUri(reqVO.getUri());
        UpdateWrapper<RobotApiUri> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", reqVO.getId());
        baseMapper.update(entity, updateWrapper);
    }

    @Override
    public void modifyStatusById(Long id) {
        // 检查是否存在要修改的任务
        checkIsExist(id);

        // 构建更新条件
        LambdaUpdateWrapper<RobotApiUri> updateWrapper = new LambdaUpdateWrapper<RobotApiUri>()
                .eq(RobotApiUri::getId, id);

        // 查询当前状态
        RobotApiUri entity = baseMapper.selectById(id);
        int currentStatus = entity.getEnable();

        // 计算新状态（0 切换为 1，1 切换为 0）
        int newStatus = currentStatus == 0 ? 1 : 0;

        // 执行更新
        baseMapper.update(null, updateWrapper.set(RobotApiUri::getEnable, newStatus));
    }

    @Override
    public void delete(Long id) {
        checkIsExist(id);
        baseMapper.delete(new LambdaQueryWrapperX<RobotApiUri>()
                .in(RobotApiUri::getId, id));
    }
}




